﻿using System;
using System.Collections.Generic;

namespace ADEVSO.Web.UI
{
    public class URLHistory
    {
        #region Variables

        private List<String> _history;
        private int _index;

        #endregion
        

        #region Constructors

        public URLHistory()
        {
            _history = new List<String>();
            _index = -1;
        }

        #endregion

        
        #region Properties


        #endregion


        #region Methods

        /// <summary>
        /// Add new page
        /// </summary>
        /// <param name="URL"></param>
        public void Add(String URL)
        {
            if (++_index <= _history.Count - 1)
                _history.RemoveRange(_index, _history.Count - _index);
            _history.Add(URL);
        }

        /// <summary>
        /// Read current page
        /// </summary>
        /// <returns></returns>
        public String Read()
        {
            return _history[_index];
        }

        /// <summary>
        /// Back to last page
        /// </summary>
        /// <returns></returns>
        public String Back()
        {
            _index--;
            return Read();
        }

        /// <summary>
        /// Back until page before page with url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public String Back(String URL)
        {
            for (int i = _index; i > -1; i--)
                if (_history[i].Equals(URL))
                    _index = --i;
            return Read();
        }

        /// <summary>
        /// Back N times
        /// </summary>
        /// <param name="TimesBack"></param>
        /// <returns></returns>
        public String Back(int TimesBack)
        {
            _index = _history.Count > 0 && TimesBack < _history.Count ? _index - TimesBack : 0;
            return Read();
        }        

        /// <summary>
        /// Read Back N times
        /// </summary>
        /// <param name="TimesBack"></param>
        /// <returns></returns>
        public String ReadBack(int TimesBack)
        {
            return _history.Count > 0 && TimesBack < _history.Count ? _history[_history.Count - TimesBack] : _history[0];
        }

        /// <summary>
        /// Forward to next page
        /// </summary>
        /// <returns></returns>
        public String Forward()
        {
            _index++;
            return Read();
        }

        /// <summary>
        /// Forward until page after page with url
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public String Forward(String URL)
        {
            for (int i = _index; i < _history.Count; i++)
                if (_history[i].Equals(URL))
                    _index = i++;
            return Read();
        }

        /// <summary>
        /// Forward N times
        /// </summary>
        /// <param name="TimesForward"></param>
        /// <returns></returns>
        public String Forward(int TimesForward)
        {
            _index = _history.Count > 0 && _index + TimesForward < _history.Count ? _index + TimesForward : _history.Count - 1;
            return Read();
        }        

        /// <summary>
        /// Read Forward N times
        /// </summary>
        /// <param name="TimesForward"></param>
        /// <returns></returns>
        public String ReadForward(int TimesForward)
        {
            return _history.Count > 0 && _index + TimesForward < _history.Count ? _history[_index + TimesForward] : _history[_history.Count - 1];
        }

        #endregion
    }
}
